<?php

class Mod_menu extends CI_Controller {

    private $menu = '';
    private $menu1 = '';
    private $paren = array();
    public $rear = '';

    function __construct() {
        parent::__construct();
        if($this->session->userdata('ngonngu') && $this->session->userdata('ngonngu')=='_en') {
            $this->rear = '_en';
            $this->lang->load('atta','english');
        }else{
            $this->lang->load('atta','vietnam');
        } 
    }

    function index() {
        $menu = $this->common_model->getdata('danhmuctt', array('publish' => 1, 'position' => 1), '', array('order', 'ASC'));
        // get all category ->menu
        $dat = $this->common_model->getdata('danhmuctt', array('publish' => 1, 'position' => 1), '', array('order', 'ASC'));
        if ($dat) {
            foreach ($dat as $v) {
                $data[$v->parentid][] = $v;
            }
            $this->menucreat($data, 0);
            echo $this->menu;
        }
    }

    function menucreat($data, $id = 0) {
        $this->menu .= '<div class="top-head menu" >';
        $selectAlias = $this->common_model->getdata('alias', array('type' => 'danhmuctt'));
        foreach ($selectAlias as $ali) {
            $dataAlias[$ali->a_id] = $ali;
        }
        $selectAliasNews = $this->common_model->getdata('alias', array('type' => 'tintuc'));
        foreach ($selectAliasNews as $ali) {
            $dataAliastt[$ali->a_id] = $ali;
        }
        $titlePr = 'title'.$this->rear;
        $aliasPr = 'alias'.$this->rear;
        $i = 1;
        $j = count($data[$id]);
        if ($id == 0) {
            $this->menu .= '<ul class="menu-ul">';
            $this->menu .='<li class="menu-li home">';
            $this->menu .= ' <a title="Trang chủ"  href="' . base_url() . '"><img src="'.base_url().'skin/images/icon-home.png"/></a>';
            $this->menu .='</li >';
        } else
            $this->menu .= '<ul class="menu-ul">';
        foreach ($data[$id] as $dulieu) {
            if ($dulieu->rel == 1) {
                $flow = 'rel="nofollow"';
            } else {
                $flow = '';
            }
            if ($j > 1) {
                $this->menu .='<li class="menu-li" >';
                $this->menu .='<a ' . $flow . ' title="' . $dulieu->title_seo . '" href="' . base_url() . $dataAlias[$dulieu->id]->$aliasPr . '.html">' . $dulieu->$titlePr . '</a>';
                if (!empty($data[$dulieu->id])) {
                    $this->menucreat_1($data, $dulieu->id, $dataAlias);
                }
                $this->menu .='</li>';
            }
            if ($j == 1) {
                $this->menu .='<li class="menu-li last">';
                $this->menu .='<a ' . $flow . ' title="' . $dulieu->title_seo . '" href="' . base_url() . $dataAlias[$dulieu->id]->$aliasPr . '.html">' . $dulieu->$titlePr . '</a>';
                if (!empty($data[$dulieu->id])) {
                    $this->menucreat_1($data, $dulieu->id, $dataAlias);
                } else {
                   $this->menucreat_2($dulieu->id, $dataAliastt);
                }
                $this->menu .='</li>';
            }


            $j--;
        }
        if ($id == 0) {
            $this->menu .= '</ul>';
        } else
            $this->menu .= '</ul>';

        $this->menu .= '</div>';
    }

    function menucreat_1($data, $id = 0, $dataAlias) {
        $titlePr = 'title'.$this->rear;
        $aliasPr = 'alias'.$this->rear;
        $f = count($data[$id]);
        if (!empty($data[$id])) {
            if ($id == 0) {
                $this->menu .= '<ul class="menu-chil">';
            } else
                $this->menu .= '<ul class="menu-chil">';
            foreach ($data[$id] as $dulieu) {

                if ($dulieu->rel == 1) {
                    $flow = 'rel="nofollow"';
                } else {
                    $flow = '';
                }
                $this->menu .='<li class="menu-chil-li">';
                $this->menu .= '<a  title="link home " ' . $flow . ' title="' . $dulieu->title_seo . '" href="' . base_url() . $dataAlias[$dulieu->id]->$aliasPr . '.html">' . $dulieu->$titlePr . '</a>';
                if (!empty($data[$dulieu->id])) {
                    $this->menucreat_1($data, $dulieu->id);
                }
                $this->menu .= '</li>';
            }
        }
        if ($id == 0) {
            $this->menu .= '</ul>';
        } else
            $this->menu .= '</ul>';
    }

    function menucreat_2($id, $dataAlias) {
        $news = $this->common_model->getdata('tintuc', array('publish' => 1, 'iddanhmuc' => $id, 'noibat' => 1), '', array('order', 'ASC'));
        $this->menu .= '<div>';
        $this->menu .= '<ul>';
        if (!empty($news)) {
            foreach ($news as $dulieu) {
                $this->menu .='<li>';
                $this->menu .= '<a title="link home "  title="' . $dulieu->title_seo . '" href="' . base_url() . $dataAlias[$dulieu->id]->$aliasPr . '.html">' . $dulieu->title . '</a>';
                $this->menu .= '</li>';
            }
        }
        $this->menu .= '</ul>';
        $this->menu .= '</div>';
    }

}
